/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filtros;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Andre
 */
public class LoginFilter implements Filter {
 private FilterConfig config;
    public void init(FilterConfig filterConfig) throws ServletException {
       this.config = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession sessao = req.getSession();
        if (sessao.getAttribute("logado") == null) {
           res.sendRedirect("login.jsp");
        } else {
            if (sessao.getAttribute("logado").equals("true")) {
                  chain.doFilter(request, response);                 
            } 
        }

    }

    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
